<?php
// INCLUDES
include_once('config.php');
include_once('QueryResult.php');  

//===============================================================
//
//======================== CLASS DB  ============================
//
//===============================================================
class DB
{

//===============================================================
//
//======================== VARIABLES ============================
//
//===============================================================

  
  

//===============================================================
//
//======================== FUNCTIONS ============================
//
//===============================================================

  
   /*********************************
   * 
   *              DB
   * 
   **********************************/
   
    public function DB()
    {
       
    }



    /********************************
    * 
    *          $ getLastestTags
    * 
    * *******************************
    */
    public function getLastestTags( $max )
    {  	 
	    $query = " SELECT tbtags.Id, tbtags.Tagname  
			   FROM   tbtags  
		       ORDER BY tbtags.Id DESC
		       LIMIT  0,{$max}" ;
  	
			
	    mysql_query("set names utf8");
		 
        $queryResult = mysql_query($query);
	
    	// Result equal zero
      	if (!$queryResult) 
    	{
    		return 0;
    	}
    	
    	 
    	$strTags_array = array();
    	while ($row = mysql_fetch_assoc($queryResult)) 
    	{ 	
    		$strTags_array[] =  $row["Tagname"] ;
    		
    	}// while
    	
    	return $strTags_array;
    }// End of getTags
    
    /********************************
    * 
    *          $ getLastestModels
    * 
    * *******************************
    */
    public function getLastestModels( $max )
    {       
        $query = " SELECT tbartifacts.Id, tbartifacts.Title, tbartifacts.Text, tbartifacts_type.Name,
                          tbusers.Name AS Author                                         
                   FROM   tbartifacts, tbartifacts_type, tbusers 
                   WHERE  tbartifacts.Type = tbartifacts_type.Id  AND tbartifacts.Author = tbusers.Id
                   ORDER BY tbartifacts.Id DESC
                   LIMIT  0,{$max}" ;
      
            
          mysql_query("set names utf8");
             
        $numresults = mysql_query($query);
         
        if (!$numresults) 
        {
            die('Invalid query: ' . mysql_error());        
        }
         
        $numrows = mysql_num_rows($numresults);
                
        
        $qResult = new QueryResult(); 
        $qResult_array = $qResult->populate($numresults); 
                     
        return $qResult_array;
    }// End of getLastestModels
    
    
    /********************************
    * 
    *          $ getLastestTypes
    * 
    * *******************************
    */
    public function getLastestTypes( $max )
    {       
        $query = " SELECT Id, Name  
                   FROM   tbartifacts_type  
                   ORDER BY Id DESC
                   LIMIT  0,{$max}" ;
      
            
        mysql_query("set names utf8");
         
        $queryResult = mysql_query($query);
    
        // Result equal zero
          if (!$queryResult) 
        {
            return 0;
        }
        
         
        $strTypes_array = array();
        while ($row = mysql_fetch_assoc($queryResult)) 
        {     
            $strTypes_array[] =  $row["Name"] ;
            
        }// while
        
        return $strTypes_array;
    }// End of getLastestTypes
    
  
    /**********************************
    * 
    *           $ getTypes
    * 
    **********************************/
    
    public function getTypes( )
    {  	
      	$query = "SELECT tbartifacts_type.Id, tbartifacts_type.Name 	          
    			  FROM   tbartifacts_type ";
    			
    	mysql_query("set names utf8");
    		 
    	$queryResult = mysql_query($query);
    	
    	// Result equal zero
      	if (!$queryResult) 
    	{
    		return 0;
    	}
    			
    	return $queryResult;
    }// End of getTypes
    
    
     /**********************************
    * 
    *           $ getAllByAuthor
    * 
    **********************************/
    public function getAllByAuthor( $authorId )
    {
         $query = "SELECT tbartifacts.Id, tbartifacts.Title, tbartifacts.Text, tbartifacts_type.Name,
                          tbusers.Name AS Author                                         
                   FROM   tbartifacts, tbartifacts_type, tbusers
                   WHERE  $authorId = tbartifacts.Author AND tbartifacts.Type = tbartifacts_type.Id  AND 
                          tbartifacts.Author = tbusers.Id";
                
        mysql_query("set names utf8");
             
        $numresults = mysql_query($query);
         
        if (!$numresults) 
        {
            die('Invalid query: ' . mysql_error());        
        }
         
        $numrows = mysql_num_rows($numresults);
                
        
        $qResult = new QueryResult(); 
        $qResult_array = $qResult->populate($numresults); 
                     
        return $qResult_array;
        
        
    }// End of getAllByAuthor 
    
  
    /**********************************
    * 
    *           $ getSearch
    * 
    **********************************/
    function getSearch( $q )
    {
        $query_aux = "(SELECT tbTags.Fkidartifacts
	                   FROM tbtags
	                   WHERE  tbtags.Tagname LIKE \"%$q%\" )" ;
	
        $query = "SELECT tbartifacts.Id, tbartifacts.Title, tbartifacts.Text, tbartifacts_type.Name, 
	              tbusers.Name AS Author
			      FROM tbartifacts, tbartifacts_type, tbusers 
			      WHERE
                 (tbartifacts.Title LIKE \"%$q%\" OR tbartifacts.Text LIKE \"%$q%\" OR
				  tbartifacts.Id IN {$query_aux})
			      AND tbartifacts.Type = tbartifacts_type.Id AND tbartifacts.Author = tbusers.Id";
                	 
    	$numresults = mysql_query($query);
    	 
    	if (!$numresults) 
    	{
    		die('Invalid query: ' . mysql_error());		
    	}
    	 
    	$numrows = mysql_num_rows($numresults);
                
        
        $qResult = new QueryResult(); 
        $qResult_array = $qResult->populate($numresults); 
                     
        return $qResult_array;
        
    }//getSearch
    
    /**********************************
    * 
    *           $ setNewType
    * 
    **********************************/
    public function setNewType( $typename )
    {
        
        $query	     =	'SELECT * FROM tbartifacts_type WHERE Name ="'.$typename.'"';
		$numresults	 =	mysql_query($query);
				
		if(mysql_num_rows($numresults)>0)
		{
			echo 0; // NOT CREATED						
		}
		else
		{		
			$query	    =	'INSERT INTO tbartifacts_type (Name) 
			                     VALUES (  "'.$typename.'" )';
			$numresults	=	mysql_query($query);
            if (!$numresults) 
        	{
        	    echo 0;
        		die('Invalid query: ' . mysql_error());		
        	}
			$lastid = mysql_insert_id();
            
			echo $lastid; // SUCCESS - CREATED
		} 
        
    }// End of setNewType
  
}// End of DB Class 
?>